package com.joysuch.wwyt.risk.repository;

import com.joysuch.wwyt.risk.entity.RiskPointEvaluationBusiness;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

public interface RiskPointEvaluationBusinessDao extends JpaRepository<RiskPointEvaluationBusiness, Long> {

    @Query(value = "select * from `wwyt_risk_point_business_evaluation` where  point_id = ?1 and point_analyze_type = ?2 and analyze_way_id =?3 and delete_flag = '0'order by evaluation_date desc,id desc limit 1 ", nativeQuery = true)
    RiskPointEvaluationBusiness getPointEvaluation(Long riskPointId, Integer pointAnalyzeType, Long analyzeWayId);

    @Query(value = "select * from `wwyt_risk_point_business_evaluation` where point_id = ?1 and point_analyze_type = ?2 and delete_flag = '0' order by evaluation_date desc ,id desc limit 1  ", nativeQuery = true)
    RiskPointEvaluationBusiness getLastPointEvaluation(Long pointId, int i);

    @Query(value = "select * from `wwyt_risk_point_business_evaluation` where point_id = ?1 and point_analyze_type = ?2 and analyze_way_id =?3 and delete_flag = '0' order by evaluation_date desc ,id desc limit 1  ", nativeQuery = true)
    RiskPointEvaluationBusiness getLastPointEvaluationByPointIdAndAnalyzeWayId(Long pointId, int i, Long analyzeWayId);

    List<RiskPointEvaluationBusiness> findByAnalyzeWayId(Long wayId);

    @Query(value = "select min(RISK_LEVEL) FROM wwyt_risk_point_business_evaluation where ID in :idList AND DELETE_FLAG='0'", nativeQuery = true)
    Integer getLibraryMinPointLevel(List<Long> idList);

    @Query(value = "select * from `wwyt_risk_point_business_evaluation` where point_id = ?1 and point_analyze_type = 1 and delete_flag = '0' order by evaluation_date desc ,id desc ", nativeQuery = true)
    List<RiskPointEvaluationBusiness> getHistoryEvaluationData(Long riskPointId);

    @Query(value = "SELECT MAX(eval.evaluation_date) FROM wwyt_risk_point_business_evaluation eval INNER JOIN wwyt_risk_point p ON eval.point_id = p.id WHERE p.RISK_DISTRICT_ID =?1 AND (p.shared IS NULL OR p.shared =0) AND p.DELETE_FLAG='0' AND p.status =3 AND (p.show_state IS NULL OR p.show_state !=44) AND eval.point_analyze_type=1 AND eval.delete_flag ='0'", nativeQuery = true)
    Date getLastEvalDateByDistrictId(Long id);

    @Query(value = "SELECT * FROM `wwyt_risk_point_business_evaluation` WHERE point_analyze_type =1 AND delete_flag = '0'AND point_id IN (SELECT id FROM wwyt_risk_point WHERE risk_district_id = ?1  AND (shared IS NULL OR shared =0) AND DELETE_FLAG='0' AND STATUS =3 AND (show_state IS NULL OR show_state !=44))", nativeQuery = true)
    List<RiskPointEvaluationBusiness> findByDistrictId(Long id);

    @Query(value = "select * from wwyt_risk_point_business_evaluation where point_id in ?1 and delete_flag = '0'", nativeQuery = true)
    List<RiskPointEvaluationBusiness> findByPointIdList(List<Long> riskPointIds);

    @Query(value = "select count(id) from wwyt_risk_point_business_evaluation where point_id = ?1 and DELETE_FLAG = '0'", nativeQuery = true)
    int countByPointId(Long id);

}
